package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-twin-sum-of-a-linked-list/description/
 * @create 2025/9/26 20:50
 * @since JDK17
 */

public class Solution71 {
    public int pairSum(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null) {  // 因为是偶数
            fast = fast.next.next;
            slow = slow.next;
        }
        fast = slow;
        ListNode pre = null, next;
        while (fast != null) {
            next = fast.next;
            fast.next = pre;
            pre = fast;
            fast = next;
        }
        int ans = 0;
        while (pre != null) {
            ans = Math.max(ans, head.val + pre.val);
            head = head.next;
            pre = pre.next;
        }
        return ans;
    }
}
